package com.mornd.gulimall.ware.controller;

import com.mornd.gulimall.common.core.result.AjaxResult;
import com.mornd.gulimall.common.core.validation.UpdateGroup;
import com.mornd.gulimall.ware.entity.WareInfo;
import com.mornd.gulimall.ware.service.IWareInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Arrays;

import static com.mornd.gulimall.common.core.result.AjaxResult.error;
import static com.mornd.gulimall.common.core.result.AjaxResult.success;

/**
 * <p>
 * 仓库信息 前端控制器
 * </p>
 *
 * @author mornd
 * @since 2023-07-29
 */
@RestController
@RequestMapping("/ware/wareInfo")
public class WareInfoController {
    @Resource
    private IWareInfoService wareInfoService;

    @GetMapping
    public AjaxResult list(WareInfo wareInfo) {
        return success(wareInfoService.list(wareInfo));
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(wareInfoService.getById(id));
    }

    @GetMapping("/all")
    public AjaxResult all() {
        return success(wareInfoService.list());
    }

    @PutMapping
    public AjaxResult save(@RequestBody @Validated WareInfo wareInfo) {
        wareInfo.setId(null);
        if(wareInfoService.exists(null, wareInfo.getName())) {
            return error("操作失败，名称已存在");
        }
        wareInfoService.save(wareInfo);
        return success();
    }

    @PostMapping
    public AjaxResult update(@RequestBody @Validated(UpdateGroup.class) WareInfo wareInfo) {
        if(wareInfoService.exists(wareInfo.getId(), wareInfo.getName())) {
            return error("操作失败，名称已存在");
        }
        wareInfoService.updateById(wareInfo);
        return success();
    }

    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        wareInfoService.removeByIds(Arrays.asList(ids));
        return success();
    }

}
